package com.tencent.wework.list

import android.util.SparseArray
import android.view.View
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView


/**
 * Created by huangwei on 2019/5/26.
 */
open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val views: SparseArray<View> = SparseArray()
    internal var onViewRecycledListener: OnViewRecycledListener? = null
    public fun <T : View> getView(@IdRes id: Int): T {
        return (views[id] as? T) ?: run {
            val view = itemView.findViewById<T>(id)
            views.put(id, view)
            return@run view
        }
    }

    fun onViewRecycled() {
        onViewRecycledListener?.onViewRecycled()
        onViewRecycledListener = null
    }
}

internal interface OnViewRecycledListener {
    fun onViewRecycled()
}